package zheezes.eisp.data.dao;

import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import zheezes.eisp.data.orm.CalcTask;
import zheezes.eisp.data.orm.CollectTask;
import zheezes.eisp.data.orm.Concentrator;
import zheezes.eisp.data.orm.ConnState;
import zheezes.eisp.data.orm.Ct;
import zheezes.eisp.data.orm.DataBean;
import zheezes.eisp.data.orm.DataType;
import zheezes.eisp.data.orm.FramePo;
import zheezes.eisp.data.orm.FrozenData;
import zheezes.eisp.data.orm.FrozenType;
import zheezes.eisp.data.orm.GroupPo;
import zheezes.eisp.data.orm.GroupStat;
import zheezes.eisp.data.orm.Lossy;
import zheezes.eisp.data.orm.Proportion;
import zheezes.eisp.data.orm.Pt;
import zheezes.eisp.data.orm.ResMeter;
import zheezes.eisp.data.orm.ResMeterDailyFrozenData;
import zheezes.eisp.data.orm.ResMeterDailyUse;
import zheezes.eisp.data.orm.ResMeterExt;
import zheezes.eisp.data.orm.Summator;
import zheezes.eisp.data.orm.SummatorDailyFrozenData;
import zheezes.eisp.data.orm.SummatorDailyUse;
import zheezes.eisp.data.orm.TermPo;
import zheezes.eisp.data.orm.VipFrozenDataPo;


import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.engine.impl.SqlMapClientImpl;

public class DataAccessor {
	private static Logger logger = Logger.getLogger(DataAccessor.class);
	
	public static final int TYPE_NORMAL = 0;
	public static final int TYPE_GBW = 1;
	public static final int TYPE_GD = 2;
	public static final int TYPE_WT = 3;
	
	private SqlMapClient sqlMapper;
	
	private int type = TYPE_NORMAL;
	
	public void setSqlMapper(SqlMapClient sqlMapper) {
		this.sqlMapper = sqlMapper;
	}
	
	public int getType() {
		return type;
	}

	public void setType(int type) {
		this.type = type;
	}
	
	public String fixsql(String sql) {
		String suffix = null;
		switch (type) {
		case TYPE_GBW:
			suffix = "Gbw";
			break;
		case TYPE_GD:
			suffix = "Gd";
			break;
		case TYPE_WT:
			suffix = "Wt";
			break;
		}
		String fsql = suffix == null ? sql : String.format("%sFor%s", sql, suffix);
		try {
			if (((SqlMapClientImpl) sqlMapper).getMappedStatement(fsql) != null) {
				return fsql;
			}
		} catch (Exception e) {
			logger.debug(String.format("no special sql(%s)(%s)", sql, suffix));
		}
		return sql;
	}
	
	private boolean setAutoCommit(boolean autoCommit) {
		try {
			Connection conn = null;
			if((conn = sqlMapper.getCurrentConnection()) != null) {
				conn.setAutoCommit(autoCommit);
				return true;
			}
		} catch (SQLException e) {
			logger.debug(e.getMessage(), e);
		}
		return false;
	}
	
	public int beginTransaction() {
		try {
			sqlMapper.startTransaction(); // auto set autoCommit = false
			logger.debug(String.format("%s begin transaction", Thread.currentThread().getName()));
			return 0;
		}  catch (SQLException e) {
			logger.debug(e.getMessage(), e);
		}
		return -1;
	}
	
	public int commit() {
		try {
			sqlMapper.commitTransaction();
			logger.debug(String.format("%s commit transaction", Thread.currentThread().getName()));
			return 0;
		} catch (SQLException e) {
			logger.debug(e.getMessage(), e);
		} finally { // commit or rollback, you should to end transaction
			try {
				sqlMapper.endTransaction();
			} catch (SQLException e) {
				logger.debug(e.getMessage(), e);
			}
			setAutoCommit(true);
		}
		return -1;
	}
	
	public int rollback() {
		try {
			sqlMapper.endTransaction();
			logger.debug(String.format("%s rollback transaction", Thread.currentThread().getName()));
			return 0;
		} catch (SQLException e) {
			logger.debug(e.getMessage(), e);
		} finally {
			setAutoCommit(true);
		}
		return -1;
	}
	
	public Concentrator getConById(int conId) {
		try {
			return (Concentrator) sqlMapper.queryForObject(fixsql("queryConById"), conId);
		} catch (SQLException e) {
			logger.debug(e.getMessage(), e);
		}
		return null;
	}
	
	public Summator getSummatorById(int id) {
		try {
			return (Summator) sqlMapper.queryForObject("querySummatorById", id);
		} catch (SQLException e) {
			logger.debug(e.getMessage(), e);
		}
		return null;
	}

	public Summator getSummatorByCon(int conId) {
		try {
			return (Summator) sqlMapper.queryForObject("querySummatorByCon", conId);
		} catch (SQLException e) {
			logger.debug(e.getMessage(), e);
		}
		return null;
	}
	
	public Summator getSummatorByZone(int znId) {
		try {
			return (Summator) sqlMapper.queryForObject("querySummatorByZone", znId);
		} catch (SQLException e) {
			logger.debug(e.getMessage(), e);
		}
		return null;
	}
	
	public GroupPo getGroupById(int id) {
		try {
			return (GroupPo) sqlMapper.queryForObject("queryGroupById", id);
		} catch (SQLException e) {
			logger.debug(e.getMessage(), e);
		}
		return null;
	}
	
	public Lossy getLossyById(int id) {
		try {
			return (Lossy) sqlMapper.queryForObject("queryLossyById", id);
		} catch (SQLException e) {
			logger.debug(e.getMessage(), e);
		}
		return null;
	}
	
	public Proportion getProportionById(int id) {
		try {
			return (Proportion) sqlMapper.queryForObject("getProportionById", id);
		} catch (SQLException e) {
			logger.debug(e.getMessage(), e);
		}
		return null;
	}
	
	public Ct getCtById(int id) {
		try {
			return (Ct) sqlMapper.queryForObject("queryCtById", id);
		} catch (Exception e) {
			logger.debug(e.getMessage(), e);
		}
		return null;
	}
	
	public Pt getPtById(int id) {
		try {
			return (Pt) sqlMapper.queryForObject("queryPtById", id);
		} catch (Exception e) {
			logger.debug(e.getMessage(), e);
		}
		return null;
	}
	
	public DataType getDataTypeByHex(String hex) {
		try {
			return (DataType) sqlMapper.queryForObject("queryDataTypeByHex", hex);
		} catch (SQLException e) {
			logger.debug(e.getMessage(), e);
		}
		return null;
	}
	
	public GroupPo getRootGroup() {
		try{
			return (GroupPo) sqlMapper.queryForObject("queryRootGroup");
		} catch(SQLException e) {
			logger.debug(e.getMessage(), e);
		}
		return null;
	}

	public int countResMeterByCon(int conId) {
		try {
			return (Integer) sqlMapper.queryForObject(fixsql("countResMeterByCon"), conId);
		} catch (SQLException e) {
			logger.debug(e.getMessage(), e);
		}
		return -1;
	}
	
	public int countVipMeterByCon(int conId) {
		try {
			return (Integer) sqlMapper.queryForObject(fixsql("countVipMeterByCon"), conId);
		} catch (SQLException e) {
			logger.debug(e.getMessage(), e);
		}
		return -1;
	}
	
	public int countResMeterByZone(int znId) {
		try {
			return (Integer) sqlMapper.queryForObject("countResMeterByZone", znId);
		} catch (SQLException e) {
			logger.debug(e.getMessage(), e);
		}
		return -1;
	}
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public int countResMeterWithDailyFrznByCon(int conId, Date date) {
		Map map = new HashMap();
		map.put("conId", conId);
		map.put("date", date);
		try {
			return (Integer) sqlMapper.queryForObject(fixsql("countResMeterWithDailyFrznByCon"), map);
		} catch (SQLException e) {
			logger.debug(e.getMessage(), e);
		}
		return -1;
	}
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public int countVipMeterWithDailyFrznByCon(int conId, Date date) {
		Map map = new HashMap();
		map.put("conId", conId);
		map.put("date", date);
		try {
			return (Integer) sqlMapper.queryForObject(fixsql("countVipMeterWithDailyFrznByCon"), map);
		} catch (SQLException e) {
			logger.debug(e.getMessage(), e);
		}
		return -1;
	}
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public int countResMeterWithDailyUseByCon(int conId, Date date) {
		Map map = new HashMap();
		map.put("conId", conId);
		map.put("date", date);
		try {
			return (Integer) sqlMapper.queryForObject("countResMeterWithDailyUseByCon", map);
		} catch (SQLException e) {
			logger.debug(e.getMessage(), e);
		}
		return -1;
	}
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public int countResMeterWithDailyUseByZone(int znId, Date date) {
		Map map = new HashMap();
		map.put("znId", znId);
		map.put("date", date);
		try {
			return (Integer) sqlMapper.queryForObject("countResMeterWithDailyUseByZone", map);
		} catch (SQLException e) {
			logger.debug(e.getMessage(), e);
		}
		return -1;
	}
	
	public int countCutOffResMeterByCon(int conId) {
		try {
			return (Integer) sqlMapper.queryForObject("countCutOffResMeterByCon", conId);
		} catch (SQLException e) {
			logger.debug(e.getMessage(), e);
		}
		return -1;
	}
	
	@SuppressWarnings({ "unchecked" })
	public List<Concentrator> queryAllCons() {
		try {
			return sqlMapper.queryForList(fixsql("queryAllCons"));
		} catch (SQLException e) {
			logger.debug(e.getMessage(), e);
		}
		return null;
	}
	
	@SuppressWarnings({ "unchecked" })
	public List<GroupPo> queryAllGroup() {
		try {
			return sqlMapper.queryForList("queryAllGroup");
		} catch (SQLException e) {
			logger.debug(e.getMessage(), e);
		}
		return null;
	}
	
	@SuppressWarnings({ "unchecked" })
	public List<GroupPo> queryAllZone() {
		try {
			return sqlMapper.queryForList("queryAllZone");
		} catch (SQLException e) {
			logger.debug(e.getMessage(), e);
		}
		return null;
	}
	
	@SuppressWarnings({ "unchecked" })
	public List<GroupPo> queryRootGroups() {
		try {
			return sqlMapper.queryForList("queryRootGroups");
		} catch (SQLException e) {
			logger.debug(e.getMessage(), e);
		}
		return null;
	}
	
	@SuppressWarnings({ "unchecked" })
	public List<CollectTask> queryAllCollectTasks() {
		try {
			return sqlMapper.queryForList("queryAllCollectTasks");
		} catch (SQLException e) {
			logger.debug(e.getMessage(), e);
		}
		return null;
	}
	
	@SuppressWarnings({ "unchecked" })
	public List<CollectTask> queryAllQueueCollectTasks() {
		logger.debug("query all queue collect task");
		try {
			return sqlMapper.queryForList(fixsql("queryAllQueueCollectTasks"));
		} catch (SQLException e) {
			logger.debug(e.getMessage(), e);
		}
		return null;
	}
	
	@SuppressWarnings({ "unchecked" })
	public List<CalcTask> queryAllCalcTasks() {
		try {
			return sqlMapper.queryForList("queryAllCalcTasks");
		} catch (SQLException e) {
			logger.debug(e.getMessage(), e);
		}
		return null;
	}
	
	@SuppressWarnings({ "unchecked" })
	public List<CalcTask> queryAllQueueCalcTasks() {
		try {
			return sqlMapper.queryForList("queryAllQueueCalcTasks");
		} catch (SQLException e) {
			logger.debug(e.getMessage(), e);
		}
		return null;
	}
	
	@SuppressWarnings({ "unchecked" })
	public List<TermPo> queryAllTerms() {
		try {
			return sqlMapper.queryForList("queryAllTerms");
		} catch (SQLException e) {
			logger.debug(e.getMessage(), e);
		}
		return null;
	}

	@SuppressWarnings("unchecked")
	public List<FramePo> queryAllFrames() {
		try {
			return sqlMapper.queryForList("queryAllFrames");
		} catch (SQLException e) {
			logger.debug(e.getMessage(), e);
		}
		return null;
	}
	
	@SuppressWarnings("unchecked")
	public List<GroupStat> queryAllGroupStat() {
		try {
			return sqlMapper.queryForList("queryAllGroupStat");
		} catch (SQLException e) {
			logger.debug(e.getMessage(), e);
		}
		return null;
	}
	
	@SuppressWarnings("unchecked")
	public List<ConnState> queryAllConnStates() {
		try {
			return sqlMapper.queryForList("queryAllConnStates");
		} catch (SQLException e) {
			logger.debug(e.getMessage(), e);
		}
		return null;
	}
	
	@SuppressWarnings("unchecked")
	public List<GroupPo> querySubGroupById(int id) {
		try {
			return sqlMapper.queryForList("querySubGroupById", id);
		} catch (SQLException e) {
			logger.debug(e.getMessage(), e);
		}
		return null;
	}
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public List<GroupPo> queryCalcedSubGroupByIdAndDate(int id, Date date) {
		Map map = new HashMap();
		map.put("id", id);
		map.put("date", date);
		try {
			return sqlMapper.queryForList("queryCalcedSubGroupByIdAndDate", map);
		} catch (SQLException e) {
			logger.debug(e.getMessage(), e);
		}
		return null;
	}
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public List<GroupPo> queryNotCalcedSubGroupByIdAndDate(int id, Date date) {
		Map map = new HashMap();
		map.put("id", id);
		map.put("date", date);
		try {
			return sqlMapper.queryForList("queryNotCalcedSubGroupByIdAndDate", map);
		} catch (SQLException e) {
			logger.debug(e.getMessage(), e);
		}
		return null;
	}
	
	@SuppressWarnings("unchecked")
	public List<FrozenType> queryToCollTypes(Date date) {
		try {
			return sqlMapper.queryForList("queryToCollSet", date);
		} catch (SQLException e) {
			logger.debug(e.getMessage(), e);
		} catch(Exception e) {
			logger.debug(e.getMessage(), e);
		}
		return null;
	}
	
	@SuppressWarnings("unchecked")
	public List<Concentrator> queryConByGroupId(int groupId) {
		try {
			return sqlMapper.queryForList("queryConByGroupId", groupId);
		} catch (SQLException e) {
			logger.debug(e.getMessage(), e);
		}
		return null;
	}
	
	public CollectTask queryCollectTask(CollectTask task) {
		try {
			return (CollectTask) sqlMapper.queryForObject(fixsql("queryCollectTask"), task);
		} catch (SQLException e) {
			logger.debug(e.getMessage(), e);
		}
		return null;
	}

	public CalcTask queryCalcTask(CalcTask task) {
		try {
			return (CalcTask) sqlMapper.queryForObject("queryCalcTask", task);
		} catch (SQLException e) {
			logger.debug(e.getMessage(), e);
		}
		return null;
	}
	
	@SuppressWarnings("unchecked")
	public List<ResMeter> queryResMeterByCon(int conId) {
		try {
			return sqlMapper.queryForList(fixsql("queryResMeterByCon"), conId);
		} catch (SQLException e) {
			logger.debug(e.getMessage(), e);
		}
		return null;
	}
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public List<ResMeter> queryConNoDailyFrozenResMeter(int conId, Date date) {
		Map map = new HashMap();
		map.put("conId", conId);
		map.put("date", date);
		try {
			return sqlMapper.queryForList(fixsql("queryConNoDailyFrozenResMeter"), map);
		} catch (SQLException e) {
			logger.debug(e.getMessage(), e);
		} catch (Exception e) {
			logger.debug(e.getMessage(), e);
		}
		return null;
	}
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public List<ResMeter> queryConNoFrzDataResMeter(int conId, Date date) {
		Map map = new HashMap();
		map.put("conId", conId);
		map.put("date", date);
		try {
			return sqlMapper.queryForList(fixsql("queryConNoFrzDataResMeter"), map);
		} catch (SQLException e) {
			logger.debug(e.getMessage(), e);
		} catch (Exception e) {
			logger.debug(e.getMessage(), e);
		}
		return null;
	}
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public List<ResMeter> queryNoHourFrznVipMeter(int conId, Date date) {
		Map map = new HashMap();
		map.put("conId", conId);
		map.put("date", date);
		try {
			return sqlMapper.queryForList(fixsql("queryNoHourFrznVipMeter"), map);
		} catch (SQLException e) {
			logger.debug(e.getMessage(), e);
		}
		return null;
	}

	public SummatorDailyFrozenData querySummatorDailyFrozenDataByMpIdAndDate(int mpId, Date date) {
		SummatorDailyFrozenData fd = new SummatorDailyFrozenData();
		fd.setMpId(mpId);
		fd.setDate(date);
		try {
			return (SummatorDailyFrozenData) sqlMapper.queryForObject("querySummatorDailyFrozenDataByMpIdAndDate", fd);
		} catch (SQLException e) {
			logger.debug(e.getMessage(), e);
		}
		return null;
	}
	
	public SummatorDailyUse querySummatorDailyUseByMpIdAndDate(int mpId, Date date) {
		SummatorDailyUse du = new SummatorDailyUse();
		du.setMpId(mpId);
		du.setDate(date);
		try {
			return (SummatorDailyUse) sqlMapper.queryForObject("querySummatorDailyUseByMpIdAndDate", du);
		} catch (SQLException e) {
			logger.debug(e.getMessage(), e);
		}
		return null;
	}
	
	public ResMeterDailyUse queryResMeterDailyUseByMpIdAndDate(int mpId, Date date) {
		ResMeterDailyUse du = new ResMeterDailyUse();
		du.setMpId(mpId);
		du.setDate(date);
		try {
			return (ResMeterDailyUse) sqlMapper.queryForObject("queryResMeterDailyUseByMpIdAndDate", du);
		} catch (SQLException e) {
			logger.debug(e.getMessage(), e);
		} catch (Exception e) {
			logger.debug(e.getMessage(), e);
		}
		return null;
	}
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public List<ResMeterExt> queryConNeedCalcDailyUseResMeter(int conId, Date date) {
		Map map = new HashMap();
		map.put("conId", conId);
		map.put("date", date);
		try {
			return sqlMapper.queryForList("queryConNeedCalcDailyUseResMeter", map);
		} catch (SQLException e) {
			logger.debug(e.getMessage(), e);
		} catch(Exception e) {
			logger.debug(e.getMessage(), e);
		}
		return null;
	}
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public List<ResMeterExt> queryConExistDailyUseResMeter(int conId, Date date) {
		Map map = new HashMap();
		map.put("conId", conId);
		map.put("date", date);
		try {
			return sqlMapper.queryForList("queryConExistDailyUseResMeter", map);
		} catch (SQLException e) {
			logger.debug(e.getMessage(), e);
		} catch(Exception e) {
			logger.debug(e.getMessage(), e);
		}
		return null;
	}
	
	public Lossy queryLossy(Lossy lossy) {
		try {
			return (Lossy) sqlMapper.queryForObject("queryLossy", lossy);
		} catch (SQLException e) {
			logger.debug(e.getMessage(), e);
		}
		return null;
	}
	
	public GroupStat queryGroupStat(GroupStat stat) {
		try {
			return (GroupStat) sqlMapper.queryForObject("queryGroupStat", stat);
		} catch (SQLException e) {
			logger.debug(e.getMessage(), e);
		}
		return null;
	}
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public List<GroupStat> querySubGroupStatByIdAndDate(int id, Date date) {
		Map map = new HashMap();
		map.put("id", id);
		map.put("date", date);
		try {
			return sqlMapper.queryForList("querySubGroupStatByIdAndDate", map);
		} catch (SQLException e) {
			logger.debug(e.getMessage(), e);
		}
		return null;
	}
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public BigDecimal sumResMeterDailyUseByCon(int conId, Date date) {
		Map map = new HashMap();
		map.put("conId", conId);
		map.put("date", date);
		String str = null;
		try {
			str = (String) sqlMapper.queryForObject(fixsql("sumResMeterDailyUseByCon"), map);
			if(str != null) {
				return new BigDecimal(str);
			}
		} catch (SQLException e) {
			logger.debug(e.getMessage(), e);
		} 
		return new BigDecimal(0);
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	public BigDecimal sumResMeterDailyUseByZone(int znId, Date date) {
		Map map = new HashMap();
		map.put("znId", znId);
		map.put("date", date);
		String str = null;
		try {
			str = (String) sqlMapper.queryForObject(fixsql("sumResMeterDailyUseByZone"), map);
			if(str != null) {
				return new BigDecimal(str);
			}
		} catch (SQLException e) {
			logger.debug(e.getMessage(), e);
		} 
		return null;
	}
	
	public boolean isOnline(String logicAddr) {
		try {
			return (Integer) sqlMapper.queryForObject("isOnline", logicAddr) != 0;
		} catch (SQLException e) {
			logger.debug(e.getMessage(), e);
		}
		return false;
	}
	
	public int saveCollectTask(CollectTask task) {
		try {
			sqlMapper.insert(fixsql("insertCollectTask"), task);
			return 0;
		} catch (SQLException e) {
			logger.debug(e.getMessage(), e);
		}
		return -1;
	}
	
	public int saveCalcTask(CalcTask task) {
		try {
			sqlMapper.insert("insertCalcTask", task);
			return 0;
		} catch (SQLException e) {
			logger.debug(e.getMessage(), e);
		}
		return -1;
	}

	public int saveResMeterDailyFrozenData(ResMeterDailyFrozenData dd) {
		try {
			sqlMapper.insert("insertResMeterDailyFrozenData", dd);
			return 0;
		} catch (SQLException e) {
			logger.debug(e.getMessage(), e);
		}
		return -1;
	}
	
	public int saveSummatorDailyFrozenData(SummatorDailyFrozenData dd) {
		try {
			sqlMapper.insert("insertSummatorDailyFrozenData", dd);
			return 0;
		} catch (SQLException e) {
			logger.debug(e.getMessage(), e);
		}
		return -1;
	}
	
	public int saveVipFrozenData(VipFrozenDataPo fd) {
		try {
			sqlMapper.insert("saveVipFrozenData", fd);
			return 0;
		} catch (SQLException e) {
			logger.debug(e.getMessage(), e);
		}
		return -1;
	}
	
	public int saveSummatorDailyUse(SummatorDailyUse du) {
		try {
			sqlMapper.insert("insertSummatorDailyUse", du);
			return 0;
		} catch (SQLException e) {
			logger.debug(e.getMessage(), e);
		}
		return -1;
	}
	
	public int saveResMeterDailyUse(ResMeterDailyUse du) {
		try {
			sqlMapper.insert("insertResMeterDailyUse", du);
			return 0;
		} catch (SQLException e) {
			logger.debug(e.getMessage(), e);
		}
		return -1;
	}
	
	public int saveLossy(Lossy lossy) {
		try {
			sqlMapper.insert("insertLossy", lossy);
			return 0;
		} catch (SQLException e) {
			logger.debug(e.getMessage(), e);
		}
		return -1;
	}
	
	public int saveFrozenData(FrozenData fd) {
		try {
			sqlMapper.insert("saveFrozenData", fd);
			return 0;
		} catch (SQLException e) {
			logger.debug(e.getMessage(), e);
		}
		return -1;
	}
	
	public int saveGroupStat(GroupStat gs) {
		try {
			sqlMapper.insert("insertGroupStat", gs);
			return 0;
		} catch (SQLException e) {
			logger.debug(e.getMessage(), e);
		}
		return -1;
	}
	
	public int saveData(DataBean data) {
		try {
			sqlMapper.insert(fixsql("insertData"), data);
			return 0;
		} catch (SQLException e) {
			logger.debug(e.getMessage(), e);
		}
		return -1;
	}
	
	public int updateGroup(GroupPo group) {
		try {
			sqlMapper.update("updateGroup", group);
			return 0;
		} catch (SQLException e) {
			logger.debug(e.getMessage(), e);
		}
		return -1;
	}
	
	public int updateCollectTask(CollectTask task) {
		try {
			sqlMapper.update(fixsql("updateCollectTask"), task);
			return 0;
		} catch (SQLException e) {
			logger.debug(e.getMessage(), e);
		}
		return -1;
	}
	
	public int updateCalcTask(CalcTask task) {
		try {
			sqlMapper.update("updateCalcTask", task);
			return 0;
		} catch (SQLException e) {
			logger.debug(e.getMessage(), e);
		}
		return -1;
	}
	
	public int updateLossy(Lossy lossy) {
		try {
			sqlMapper.update("updateLossy", lossy);
			return 0;
		} catch (SQLException e) {
			logger.debug(e.getMessage(), e);
		}
		return -1;
	}
	
	public int updateGroupStat(GroupStat stat) {
		try {
			sqlMapper.update("updateGroupStat", stat);
			return 0;
		} catch (SQLException e) {
			logger.debug(e.getMessage(), e);
		}
		return -1;
	}
	
	public int updateSummatorDailyUse(SummatorDailyUse du) {
		try {
			sqlMapper.update("updateSummatorDailyUse", du);
			return 0;
		} catch (SQLException e) {
			logger.debug(e.getMessage(), e);
		}
		return -1;
	}
	
	public int deleteCollectTask(int id) {
		try {
			sqlMapper.delete("deleteCollectTaskById", id);
			return 0;
		} catch (SQLException e) {
			logger.debug(e.getMessage(), e);
		}
		return -1;
	}
	
	public int deleteConById(int conId) {
		try {
			sqlMapper.delete("deleteConById", conId);
			return 0;
		} catch (SQLException e) {
			logger.debug(e.getMessage(), e);
		}
		return -1;
	}
	
	public int replaceCollectTask(CollectTask task) {
		CollectTask ct = null;
		if ((ct = queryCollectTask(task)) == null) {
			return saveCollectTask(task);
		} else {
			task.setId(ct.getId());
			return updateCollectTask(task);
		}
	}
	
	public int replaceCalcTask(CalcTask task) {
		CalcTask ct = null;
		if ((ct = queryCalcTask(task)) == null) {
			return saveCalcTask(task);
		} else {
			task.setId(ct.getId());
			return updateCalcTask(task);
		}
	}
	
	public int replaceLossy(Lossy lossy) {
		Lossy ls = queryLossy(lossy);
		if(ls == null) {
			return saveLossy(lossy);
		} else {
			lossy.setId(ls.getId());
			return updateLossy(lossy);
		}
	}
	
	public int replaceGroupStat(GroupStat stat) {
		GroupStat st = queryGroupStat(stat);
		if (st == null) {
			return saveGroupStat(stat);
		} else {
			stat.setId(st.getId());
			return updateGroupStat(stat);
		}
	}
	
	public int replaceSummatorDailyUse(SummatorDailyUse du) {
		SummatorDailyUse tmp = querySummatorDailyUseByMpIdAndDate(du.getMpId(), du.getDate());
		if(tmp == null) {
			return saveSummatorDailyUse(du);
		} else {
			tmp.setId(du.getId());
			return updateSummatorDailyUse(tmp);
		}
	}
}
